﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Supersense.db;
using System.Collections;

namespace Supersense.db
{
    /// <summary>
    /// Crud 的摘要说明
    /// </summary>
    /// 
    public class Crud
    {
        public String tablename;
        public String pk;
        public Database db;


        public Crud(String tablename)
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            this.db = new Database();
            this.tablename = tablename;
            this.pk = "id";
        }
        public Crud(String tablename, String pk)
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            this.db = new Database();
            this.tablename = tablename;
            this.pk = pk;
        }

        public String fRow(String where)
        {
            String sql = "SELECT * FROM " + this.tablename + " WHERE " + where;
            return sql;
        }

        public String fRow(String where, String fields) 
        {
            String sql = "SELECT "+ fields +" FROM " + this.tablename + " WHERE " + where;
            return sql;
        }

        public DataTable fPk(int pk)
        {
            String sql = "SELECT * FROM "+ this.tablename + " WHERE "+ this.pk +"="+ pk;
            return this.db.executedt(sql);
        }

        public DataTable fList( String where , String order, String limit, String fields)
        {
            String sql = "";
            if(fields == null || fields == "")
            {
                fields = "*";            
            }

            limit = (limit=="" || limit == null) ? "" : " top "+ limit +" "; 

            sql = "SELECT  "+ limit + fields + " FROM " + this.tablename;
            
            if (where != null && where != "") 
            {
                sql += " WHERE " + where;
            }
            if (order != null && order != "")
            {
                sql += " ORDER BY " + order;
            }
            DataTable dt = this.db.executedt(sql);
            return dt;
        }

        public String fList(String where, String order, String limit, String fields, String d)
        {
            String sql = "";
            if (fields == null || fields == "")
            {
                fields = "*";
            }

            limit = (limit == "" || limit == null) ? "" : " top " + limit + " ";

            sql = "SELECT  " + limit + fields + " FROM " + this.tablename;

            if (where != null && where != "")
            {
                sql += " WHERE " + where;
            }
            if (order != null && order != "")
            {
                sql += " ORDER BY " + order;
            }
            return sql;
        }

        public int insert()
        {
            return 0;
        }

        public String update(Hashtable post)
        {
            
            String sql = "UPDATE "+  this.tablename +" SET ";
            if (post != null)
            {
                foreach (DictionaryEntry item in post)
                {
                    sql += item.Key.ToString() + "=" + item.Value.ToString();
                }
            }
            if (post.Contains(this.pk))
            {
                sql += " WHERE " + this.pk +"="+ post[this.pk] ;
            }
            //return 0;
            return sql;
        }

        public String update(Hashtable post, String where)
        {
            String sql = "UPDATE " + this.tablename + " SET ";
            if (post != null)
            {
                foreach (DictionaryEntry item in post)
                {
                    sql += item.Key.ToString() + "=" + item.Value.ToString();
                }
            }
            if (where != null && where != "")
            {
                sql += " WHERE " + where;
            }
            //return 0;
            return sql;
        }

        public String delete(int pk) 
        {
            String sql = "DELETE FROM "+ this.tablename + " WHERE "+ this.pk + "=" + pk;
            return sql;
        }

        public String delete(String where)
        {
            String sql = "DELETE FROM " + this.tablename + " WHERE " + where;
            return sql;
        }

    }
}